/*
 * @Description: 最小订单金额规则DS
 * @Version: 1.0
 * @Autor: c-jack.qian
 * @Date: 2021-02-24 11:56:54
 * @LastEditors: c-jack.qian
 * @LastEditTime: 2021-04-25 14:57:45
 */
import { DataSetSelection, FieldType } from 'choerodon-ui/pro/lib/data-set/enum';
import commonConfigStore from '@common/config/commonConfig';
import intl from 'utils/intl';
const O2_OM = process.env.NODE_ENV === 'development' ? '/o2om-c-xx' : commonConfigStore.O2_OM;

const minOrderListDataSet = () => ({
    autoQuery: true,
    selection: 'multiple',
    primaryKey: 'id',
    transport: {
        read: config => {
            return {
                ...config,
                url: `${O2_OM}/v1/djiom-order-amount-rules`,
                method: 'get',
            };
        },
        destroy: config => {
            return {
                ...config,
                url: `${O2_OM}/v1/djiom-order-amount-rules/delete`,
                method: 'post',
            };
        },
    },
    fields: [
        {
            name: 'businessGroup',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.groupName').d('组别'),
            lookupCode: 'DJICM_GROUP',
        },
        {
            name: 'largeAreaName',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.region').d('大区'),
        },
        {
            name: 'areaControy',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.applicableRegion').d('适用区域国家'),
            required: true,
        },
        {
            name: 'amountRuleType',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.amountRuleType').d('规则设置'),
            required: true,
            lookupCode: 'DJIOM.AMOUNT_RULE_TYPE'
        },

        {
            name: 'priority',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.orderPriority').d('优先级'),
        },
        {
            name: 'createdBy',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.createdUserName').d('创建人'),
        },
        {
            name: 'creationDate',
            type: FieldType.dateTime,
            label: intl.get('dji.otc.om.view.field.creationDate').d('创建时间'),
        },
    ],
    queryFields: [
        {
            name: 'businessGroup',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.groupName').d('组别'),
            lookupCode: 'DJICM_GROUP',
        },
        {
            name: 'regionId',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.region').d('大区'),
            lookupCode: 'DJICM_CHOOSE_BIG_AREA',
        },
        // {
        //     name: 'largeAreaObj',
        //     type: FieldType.object,
        //     label: intl.get('dji.otc.om.view.field.tagName').d('大区'),
        //     checkValueOnOptionsChange: true,
        //     lookupUrl: `${O2_CM}/v1/cm-contract-permissions/get-largeArea-info`,
        //     ignore: 'always',
        // },
        // {
        //     name: 'regionId',
        //     bind: 'largeAreaObj.value',
        //     type: FieldType.string,
        //     label: intl.get('dji.otc.om.view.field.tagName').d('大区'),
        // },
    ],
});

// 区域DS
const areaDateSet = () => ({
    primaryKey: 'keyId',
    selection: 'single' as DataSetSelection,
    autoQuery: true,
    paging: false,
    parentField: 'largeAreaId',
    idField: 'keyId',
    expandField: 'expand',
    checkField: 'check',
    transport: {
        read: (config) => {
            return {
                ...config,
                url: `/hitf/v1/dji-external-interface/get-regions`,
                method: 'post',
                transformResponse: value => {
                    const reqData = JSON.parse(value);
                    const reqareaList = reqData.data
                    const largeList: any = []
                    const areaList: any = []
                    // 去重obj
                    let obj = {}

                    // 去重多个小区
                    let objArea = {}
                    let id = 100
                    reqareaList.forEach(item => {
                        const key = item.largeAreaId
                        if (!obj[key] && item.largeAreaId) {
                            largeList.push({
                                largeAreaCnName: item.largeAreaCnName,
                                areaId: item.largeAreaId,
                                largeAreaData: true,
                                keyId: item.largeAreaId,
                            })
                            obj[key] = key;
                        }

                        const key2 = item.areaId 
                        if (!objArea[key2]) {
                            largeList.push({
                                ...item,
                                keyId: id++
                            })
                            objArea[key2] = key2;
                        }
                    })

                    const showAreaTreeList = areaList.concat(largeList)
                    console.log('showAreaTreeList',showAreaTreeList);


                    // 解决大区域没有 regionName regionId显示不了的问题
                    // const newList = reqData.map(item => {
                    //     return {
                    //         ...item,
                    //         regionName: item.regionName || item.areaName,
                    //         regionId: item.regionId || item.areaId,
                    //     };
                    // });
                    // // 按名字字母进行排序
                    // newList.sort((a, b) => { return a.regionName.charCodeAt(0) - b.regionName.charCodeAt(0); });
                    return showAreaTreeList;
                },
            };
        },
    },
});
export { minOrderListDataSet, areaDateSet };
